'''
# Coding:utf-8
# Project: iiop
# Author: rtf
# Time: 2021-04-15 17:16:49
# FileName: base64_to_image.py
# Software: PyCharm
'''

import base64

IMAGE_MIME_TYPE = {"image/gif": "gif",
                   "image/png": "png",
                   "image/jpeg": "jpg",
                   "image/svg+xml": "svg"}


def save_image(img_str, image_name):
    mime_type = img_str[img_str.find(":") + 1:img_str.find(";")]
    img_str = img_str[len(mime_type) + 13:]
    img_data = base64.b64decode(img_str)
    filename = f'../images/{image_name}.{IMAGE_MIME_TYPE.get(mime_type)}'
    with open(filename, 'wb') as f:
        f.write(img_data)
        f.close()


img_str = ""
save_image(img_str, "test")

